package org.leviatan.definator.core.registry;

import java.util.HashMap;
import java.util.Map;

import org.leviatan.common.xml.XMLIO;
import org.leviatan.definator.core.model.Field;
import org.leviatan.definator.core.model.Record;
import org.leviatan.definator.core.model.Table;
import org.leviatan.definator.core.model.impl.FieldImpl;
import org.leviatan.definator.core.model.impl.RecordImpl;
import org.leviatan.definator.core.model.impl.TableImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public abstract class RegistryFileParser {
	
	public static Document loadFile(String aFileName) {
		return XMLIO.getInstance().readXMLFile(aFileName);
	}
	
	public static Map parseTableDOM(Document doc) {
		HashMap<String, Table> tableDefMap = new HashMap<String, Table>();
		NodeList nodeList = doc.getElementsByTagName("table");
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			Element tableDefElem = (Element) nodeList.item(i);
			
			String idStr = tableDefElem.getElementsByTagName("id").item(0).getTextContent();
			String name = tableDefElem.getElementsByTagName("name").item(0).getTextContent();;
			
			/*
			String dbUrl = dataSourceDefElem.getElementsByTagName("database-url").item(0).getTextContent();;
			String dbName = dataSourceDefElem.getElementsByTagName("database-name").item(0).getTextContent();;
			String dbTableName = dataSourceDefElem.getElementsByTagName("table-name").item(0).getTextContent();;
			String userName = dataSourceDefElem.getElementsByTagName("user-name").item(0).getTextContent();;
			String userPass = dataSourceDefElem.getElementsByTagName("user-password").item(0).getTextContent();;
			*/
			
			
			Table tbl = new TableImpl();
			tbl.setId(idStr);
			tbl.setName(name);
			
			tableDefMap.put(idStr, tbl);
			
			System.out.println("pass");
			
		}
		return tableDefMap;
	}
	
	public static Map parseRecDOM(Document doc) {
		HashMap<String, Record> recordDefMap = new HashMap<String, Record>();
		NodeList nodeList = doc.getElementsByTagName("record");
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			Element dataSourceDefElem = (Element) nodeList.item(i);
			
			String idStr = dataSourceDefElem.getElementsByTagName("id").item(0).getTextContent();
			String name = dataSourceDefElem.getElementsByTagName("name").item(0).getTextContent();;
			String dsRefIdStr = dataSourceDefElem.getElementsByTagName("table-ref-id").item(0).getTextContent();;
			
			//System.out.println(idStr);
			
			Record rec = new RecordImpl();
			rec.setId(idStr);
			rec.setName(name);
			rec.setTableRef(dsRefIdStr);
			
			recordDefMap.put(idStr, rec);
			
		}
		return recordDefMap;
	}
	
	public static Map parseFieldDOM(Document doc) {
		HashMap<String, Field> fieldDefMap = new HashMap<String, Field>();
		NodeList nodeList = doc.getElementsByTagName("field");
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			Element dataSourceDefElem = (Element) nodeList.item(i);
			
			String idStr = dataSourceDefElem.getElementsByTagName("id").item(0).getTextContent();
			String name = dataSourceDefElem.getElementsByTagName("name").item(0).getTextContent();;
			String recRefIdStr = dataSourceDefElem.getElementsByTagName("record-ref-id").item(0).getTextContent();;
			String dataType = dataSourceDefElem.getElementsByTagName("data-type").item(0).getTextContent();;
			String dataLengthStr = dataSourceDefElem.getElementsByTagName("data-length").item(0).getTextContent();;
			//String name = dataSourceDefElem.getElementsByTagName("text-data").item(0).getTextContent();;
			//String name = dataSourceDefElem.getElementsByTagName("name").item(0).getTextContent();;
			
			
			/* Special treat for Integers */
			int dataLength = Integer.parseInt(dataLengthStr);
			
			Field f = new FieldImpl();
			f.setId(idStr);
			f.setName(name);
			f.setRecordRef(recRefIdStr);
			f.setType(dataType);
			f.setLength(dataLength);
			
			fieldDefMap.put(idStr, f);
			
		}
		
		return fieldDefMap;
	}
}
	
